package at.codebase.web.templates;

import at.codebase.db.entities.EItemFeedback.EItemFeedbackRating;
import at.codebase.security.SecureStringBuilder;
import at.codebase.web.util.template.Template;
import at.codebase.web.util.template.TemplateEngine;

/**
 * 
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 *
 */
public class RubyRatingBar extends Template {

	private static String TEMPLATE_URL = "util/ruby_rating_bar.html";

	/*
	 * Between 0 and 5
	 */
	public RubyRatingBar(double rating, int ratingTotal) {
		super(TemplateEngine.getTemplateContent(TEMPLATE_URL));
		setDoubleRating(rating, ratingTotal);
	}

	public RubyRatingBar(EItemFeedbackRating rating) {
		super(TemplateEngine.getTemplateContent(TEMPLATE_URL));
		setEnumRating(rating);
	}
	
	private void setDoubleRating(double rating, int ratingTotal) {
		SecureStringBuilder builder = new SecureStringBuilder();
		for(int i = 0; i < 5; i++){
			double c = rating - i;
			if(c > 0.5){
				builder.appendInsecure("<img src=\"/images/rating/ruby_full.png\">\n");
			}else if(c <= 0.5 && c > 0){
				builder.appendInsecure("<img src=\"/images/rating/ruby_half.png\">\n");
			}else{
				builder.appendInsecure("<img src=\"/images/rating/ruby_empty.png\">\n");
			}
			
		}
		builder.appendInsecure("<span>( <a href=\"#feedback\">"+ratingTotal+"</a> )</span>");
		setHTML("rubyList", builder.toString());
	}
	
	private void setEnumRating(EItemFeedbackRating rating){
		SecureStringBuilder builder = new SecureStringBuilder();
		int count = 0;
		switch (rating) {
			case RUBY_1:
				count = 1;
				break;
			case RUBY_2:
				count = 2;
				break;
			case RUBY_3:
				count = 3;
				break;
			case RUBY_4:
				count = 4;
				break;
			case RUBY_5:
				count = 5;
				break;
			case NO_RATING:
				count = 0;
				break;
		}

		for(int i = 0; i < 5; i++){
			if(i < count){
				builder.appendInsecure("<img src=\"/images/rating/ruby_full.png\">\n");
			}else{
				builder.appendInsecure("<img src=\"/images/rating/ruby_empty.png\">\n");
			}
		}
		
		setHTML("rubyList", builder.toString());
	}
	
}
